#pragma once

#include<unordered_map>
#include <iostream>
#include <string>
#include <string>
#include "TcpServer.hpp"
#include "HttpProtocol.hpp"
#include<functional>
using namespace TcpServerModule;
using http_handler_t=std::function<void(HttpRequest&,HttpResponse&)>;
class HttpServer
{
public:
    HttpServer(int port)
        : _tsvr(std::make_unique<TcpServer>(port))
    {
    }
    void Register(std::string name,http_handler_t handler)
    {
        _route[name]=handler;
    }
    bool SafeCheck(std::string service)
    {
        auto iter=_route.find(service);
        if(iter==_route.end())
        return false;
        return true;
    }
    void Start()
    {
        _tsvr->InitHandler([this](SockPtr sockfd, InetAddr addr)
                           { return this->HandlerHttpRequest(sockfd, addr); });
        _tsvr->Loop();
    }
    bool HandlerHttpRequest(SockPtr sockfd, InetAddr addr)
    {
        LOG(LogLevel::INFO) << "get a new client" << sockfd->Fd() << "addr info" << addr.Addr();
        std::string http_request;
        // std::cout << http_request << "\n";
        sockfd->Recv(&http_request); // 这里不做报文完整性的处理
        HttpRequest req;
        req.Deserialize(http_request);
        std::cout << "用户想要" << req.URL() << "\n";
        HttpResponse res;
        //需求被分为两种，一种是没有参数的，属于静态资源的请求，一种是有参数的，属于动态资源的请求
        //如果是静态资源的请求，直接返回文件内容，如果是动态资源的请求，则需要解析参数，然后返回处理结果
        if(req.IsHashArgs())
        {
            LOG(LogLevel::INFO)<<"请求判断完毕";
            if(SafeCheck(req.Path()))
            {
                LOG(LogLevel::INFO)<<"参数合理性判断完毕";
            _route[req.Path()](req,res);
            LOG(LogLevel::INFO)<<"----------------------------------";
            req.Print();
            LOG(LogLevel::INFO)<<"----------------------------------";

            }
            else
            res.BuildResponse(req);
        
        }
        else
        {
            LOG(LogLevel::INFO)<<"请求判断完毕";
            res.BuildResponse(req);
        }
        // 构建回复
        
        std::string rep_str;
        res.Serialize(&rep_str);
        sockfd->Send(rep_str);
        // 分析请求
        // 提取完整的请求
        // 进行序列化和反序列化
        //     std::string status_line = "HTTP/1.1 200 OK\r\n";
        //    // status_line += "Content-type: text/html\r\n";
        //     std::string body = R"(<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Hello World</title></head><body><h1>Hello World</h1></body></html>)";
        //     int len = body.size();
        //    // status_line += "Content-Length: " + std::to_string(len) + "\r\n";
        //     status_line += "\r\n";
        //     std::string http_response=status_line+body;

        // std::cout << http_response << std::endl;
        // sockfd->Send(http_response);
        return true;
    }

    ~HttpServer() {}

private:
std::unordered_map<std::string,http_handler_t> _route;;
    std::unique_ptr<TcpServer> _tsvr;
};